package com.ruoyi.framework.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.cas.web.CasAuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Author: Zhangjl
 * Created: 2023/8/22 13:19
 */
public class CasAuthenticationRedirectExtends extends CasAuthenticationEntryPoint {

    private static final Logger logger = LoggerFactory.getLogger(CasAuthenticationRedirectExtends.class);

    private String serviceUrlBak=null;

    @Override
    protected String createServiceUrl(final HttpServletRequest request, final HttpServletResponse response) {
        //        if(serviceUrlBak==null){
        //            serviceUrlBak=getServiceProperties().getService();
        //        }else{
        //            String ctx=request.getContextPath();
        //            String requestURI=request.getRequestURI();
        //            requestURI=requestURI.substring(requestURI.indexOf(ctx)+ctx.length(),requestURI.length());
        //            String sessionid = request.getSession().getId();
        //            request.getSession().setAttribute(sessionid,requestURI);
        //        }
        String redirectUri = request.getParameter("redirect_uri");
        String ticket = request.getParameter("ticket");
        serviceUrlBak=getServiceProperties().getService();
        String sessionId = request.getSession().getId();
        request.getSession().setAttribute(sessionId,redirectUri);
        request.getSession().setAttribute("_ticket",ticket);

        // 可以通过获取 request中的queryString参数，作为CasAuthenticationSuccessHandler # onAuthenticationSuccess 中 fromHttpUrl#service
        //无需像这样拼接  queryParam("service", casProperties.getAppServerUrl() + "/?redirect_uri="+targetUrl)
        //
        //CasAuthenticationSuccessHandler # onAuthenticationSuccess 可改写成这样灵活的设置
        // Object queryParameter = httpSession.getAttribute("_queryParameter");
        // queryParam("service", casProperties.getAppServerUrl() + queryParameter)
        // >>>>>>>
        //        Map<String, String[]> parameterMap = request.getParameterMap();
        //        StringBuilder sb = new StringBuilder();
        //        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
        //            if(entry.getKey().equalsIgnoreCase("ticket")){
        //                continue;
        //            }
        //            sb.append(entry.getKey()).append("=").append(entry.getValue()[0]).append("&");
        //        }
        //        String substring = sb.substring(0, sb.length() - 1);
        //        logger.info("_queryParameter:{}",substring);
        //        request.getSession().setAttribute("_queryParameter",substring);
        // <<<<<<<<<

        return super.createServiceUrl(request, response);
    }

    @Override
    protected String createRedirectUrl(String serviceUrl) {
        return super.createRedirectUrl(serviceUrl);
    }
}